<?php
/*
* Input:  Filters object
* return: params array for use in query, or functions for direct PDO filtering
* 
*
*/

class CF_FilterQueryPdo {

static function filter_parameters($filters){
	if(is_array($filters)){
	foreach($filters as $filterparam => $filter){
	if(!empty($filter)){
		if(!empty($filter['source'])){
			$param['var'] = $filter['source'] . '.' . $filter['var'];
		}else{
			$param['var'] = $filter['var'];
		}
		$param['val'] = $filter['val'];
		$param['op'] = $filter['op'];
		
		$params[] = $param;
		
	}
	}
	}
	
	return $params;

}

	/**
	 * make_query_PSfilter will create a  prepare staement placeholder query filter according to fieldspec settings, Request input, and Session data
	 *
	 * $this->filters[filterparam][var] = column variable to filter
	 * $this->filters[filterparam][val] = current value
	 * $this->filters[filterparam][op] = operation of filter, to later use in query (=,<,>, etc)
	 * $this->filters[filterparam][source] = table (or alias) source for filter
	 */	 
	function make_query_PSfilter(){
		if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filterparam => $filter){
				if(isset($filter['source'])){
					$tabledot = $filter['source'] . '.';
				}
				$flt_text .= " AND $tabledot" . $filter['var'] . ' ' . $filter['op'] . " :FLT" . $filter['var'] . " ";
				
			}	
		}
	
	return $flt_text;
	}
	
	
	function make_PSfilter_bindings(&$stmt){
			if(!empty( $this->filters)) {
            $tabledot = '';
			foreach($this->filters AS $filtername => $filter){
                $currentvar = $filter['var'];
				if(!empty($this->fieldspec[$currentvar]['filter'])){
					if(isset($this->fieldspec[$currentvar]['source'])){
							$tabledot = $this->fieldspec[$currentvar]['source'] . '.';
						}
					
						$bindvar = ':FLT'. $filter['var'];
						$value = $filter['val'];
						if($filter['op'] == 'LIKE' AND $this->keyword_filter == true){
							$value = '%' . $value . '%';
							}							

						$stmt->bindValue($bindvar,$value);
				}
			}	
		}
	}
	
}
?>